//声明函数        执行器   『同步回调』

function Promise(executor) {
    //设置实例对象的属性
    this.PromiseState = 'pending';
    this.PromiseResult = undefined;
    this.callbacks = [];

    //声明函数
    const success = (value) => {
        if (this.PromiseState !== 'pending') return;
        //修改状态为成功  PromiseState
        this.PromiseState = 'fulfilled';
        //设置成功的结果值 PromiseResult
        this.PromiseResult = value;
        //调用回调
        if (this.callbacks.length > 0) {
            this.callbacks.forEach(cbObj => {
                //执行成功的回调
                cbObj.ok();
            })
        }
    }

    const fail = (reason) => {
        if (this.PromiseState !== 'pending') return;
        //修改状态为失败  PromiseState
        this.PromiseState = 'rejected';
        //设置成功的结果值 PromiseResult
        this.PromiseResult = reason;
        //调用回调
        if (this.callbacks.length > 0) {
            this.callbacks.forEach(cbObj => {
                //执行成功的回调
                cbObj.ng();
            })
        }
    }

    try {
        //调用执行器函数
        executor(success, fail);
    } catch (e) {
        //调用 fail 函数
        fail(e);
    }
}

Promise.prototype.then = function (onFulfilled, onRejected) {

    //判断  异常穿透
    if(typeof onRejected !== 'function'){
        onRejected = reason => {throw reason};
    }

    //值传递
    if(typeof onFulfilled !== 'function'){
        onFulfilled = value => value;
    }

    return new Promise((resolve, reject) => {
        //封装函数简化代码
        let callback = (type) => {
            try {
                let res = type(this.PromiseResult);
                //判断
                if (res instanceof Promise) {
                    res.then(v => {
                        resolve(v);
                    }, r => {
                        reject(r);
                    })
                } else {
                    resolve(res);
                }
            } catch (e) {
                reject(e);
            }
        }

        //判断 成功
        if (this.PromiseState === 'fulfilled') {
            setTimeout(() => {
                callback(onFulfilled);// 
            })
        }

        //失败
        if (this.PromiseState === 'rejected') {
            setTimeout(() => {
                callback(onRejected);
            })
        }

        //pending 
        if (this.PromiseState === 'pending') {
            //保存回调
            this.callbacks.push({
                ok: function () {
                    //成功
                    callback(onFulfilled);
                },
                ng: function () {
                    callback(onRejected);
                }
            })

        }

    });
}

Promise.prototype.catch = function(onRejected){
    return this.then(undefined, onRejected);
}

Promise.resolve = function(value){
    return new Promise((resolve,reject) => {
        //判断
        if(value instanceof Promise){
            value.then(resolve, reject);
            value.then(v=>{
                resolve(v);
            }, r => {
                reject(r);
            });
        }else{
            resolve(value);
        }
    })
}

Promise.reject = function(reason){
    return new Promise((resolve, reject) => {
        reject(reason);
    })
}


//覆盖全局的 alert
// function alert(){
//     console.log(123);
// }

//为实例对象添加属性
// function Person(age){
//     this.age = age;
// }
// let me = new Person(18);

